//
//  Crypto.swift
//  PerfectServer
//
//  Created by 王薰怡 on 2017/6/28.
//
//

import Foundation
import PerfectCrypto

struct Crypto {
    
    static func encrypt(_ password: String) -> String {
        let random = [UInt8](randomCount: 1024)
        if let base64 = random.encode(.base64),
            let base64Str = String(validatingUTF8: base64) {
            print(base64Str)
        }
        return ""
    }
    
    static func decrypt(_ password: String) -> String {
        return ""
    }
    
    
    static func generateSalt() -> String? {
        let random = [UInt8](randomCount: 32)
        if let base64 = random.encode(.base64),
            let base64Str = String(validatingUTF8: base64) {
            return base64Str
        }
        return nil
    }
    
    static func generateToken() -> String? {
        let random = [UInt8](randomCount: 64)
        if let base64 = random.encode(.base64),
            let base64Str = String(validatingUTF8: base64) {
            return base64Str
        }
        return nil
    }
    
}

extension String {
    var md5String : String? {
        if let md5Bytes = self.digest(.md5)?.encode(.hex),
            let md5Str = String(validatingUTF8: md5Bytes) {
            return md5Str
        }
        return nil
    }
    
    var sha256String : String? {
        if let sha256Bytes = self.digest(.sha256)?.encode(.hex),
            let sha256Str = String(validatingUTF8: sha256Bytes) {
            return sha256Str
        }
        return nil
    }
}

